package cn.gwm.flink.streaming.dwm.charge.assist.gps;

import cn.gwm.flink.streaming.constant.BaseFields;
import cn.gwm.flink.streaming.dwm.charge.bean.ChargeArgs;
import cn.gwm.flink.streaming.dwm.charge.util.SupplyUtil;
import cn.hutool.json.JSONObject;

import java.util.Map;

/**
 * @ClassName HbaseGpsMatch
 * @Description 从hbase中进行匹配
 * @Author LiangGuang
 * @Date 2023/06/28 15:25
 */
public class HbaseGpsMatch extends GpsMatch {
    /**
     * 匹配时间 往前推5分钟
     */
    private static Long LONG_TIME = 5 * 60 * 1000L;

    public void match(JSONObject summar, ChargeArgs args) throws Exception {
        if (super.getNext() != null) {
            super.getNext().match(summar, args);
        }
        if (!summar.containsKey(BaseFields.LATITUDE) || BaseFields.defaultVal.equals(summar.getStr(BaseFields.LATITUDE))) {
            JSONObject nowdata = args.getRealNowData();
            // 时间范围，插枪前5分钟 截止到目前时间
            Long startTime = summar.getLong("chrgConnectTid");
            if (startTime == null) {
                startTime = nowdata.getLong(BaseFields.tid);
            }
            Map<String, Double> gps = SupplyUtil.getGpsByVinAndTid(args.getPhoenixTableName(), nowdata.getStr(BaseFields.vin),
                    startTime - (LONG_TIME), nowdata.getLong(BaseFields.tid));
            if (gps.size() > 0) {
                summar.set(BaseFields.LATITUDE, "0".equals(gps.get(BaseFields.LATITUDE)) ? BaseFields.defaultVal : gps.get(BaseFields.LATITUDE));
                summar.set(BaseFields.LONGITUDE, "0".equals(gps.get(BaseFields.LONGITUDE)) ? BaseFields.defaultVal : gps.get(BaseFields.LONGITUDE));
            }
        }
    }
}
